﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Brjnk.Common;

namespace Brjnk.GpxExplorer.UI
{
    public partial class PaperSizePicker : UserControl, INotifyPropertyChanged
    {
        public PaperSizePicker()
        {
            InitializeComponent();
            mainGrid.DataContext = this;
            PredefinedPaper = ((IEnumerable<PaperSize>)App.Instance.FindResource("PaperSizes")).First();
            this.LostFocus += new RoutedEventHandler(PaperSizePicker_LostFocus);
        }

        private void PaperSizePicker_LostFocus(object sender, RoutedEventArgs e)
        {
            mainGrid.BindingGroup.CommitEdit();
            HasError = Validation.GetHasError(mainGrid);
            ProcessChange();
        }

        private bool landscape;

        public bool Landscape
        {
            get { return landscape; }
            set { landscape = value; OnPropertyChanged("Landscape"); ProcessChange(); }
        }

        private PaperSize predefinedPaper;

        public PaperSize PredefinedPaper
        {
            get { return predefinedPaper; }
            set { predefinedPaper = value; OnPropertyChanged("PredefinedPaper"); ProcessChange(); }
        }

        private bool useCustomSize;

        public bool UseCustomSize
        {
            get { return useCustomSize; }
            set { useCustomSize = value; OnPropertyChanged("UseCustomSize"); ProcessChange(); }
        }

        private Distance customPaperWidth = 0.21;

        public Distance CustomPaperWidth
        {
            get { return customPaperWidth; }
            set { customPaperWidth = value; OnPropertyChanged("CustomPaperWidth"); ProcessChange(); }
        }

        private Distance customPaperHeight = 0.297;

        public Distance CustomPaperHeight
        {
            get { return customPaperHeight; }
            set { customPaperHeight = value; OnPropertyChanged("CustomPaperHeight"); ProcessChange(); }
        }

        public PaperSize SelectedPaper
        {
            get { return (PaperSize)GetValue(SelectedPaperProperty); }
            set { SetValue(SelectedPaperProperty, value); }
        }

        public static readonly DependencyProperty SelectedPaperProperty =
            DependencyProperty.Register("SelectedPaper", typeof(PaperSize), typeof(PaperSizePicker), new UIPropertyMetadata(null));

        private bool hasError;

        public bool HasError
        {
            get { return hasError; }
            set { hasError = value; OnPropertyChanged("HasError"); }
        }

        private void ProcessChange()
        {
            if (!Validation.GetHasError(this))
            {
                if (useCustomSize)
                {
                    SelectedPaper = new PaperSize() { Width = CustomPaperWidth * 100, Height = CustomPaperHeight * 100 };
                }
                else
                {
                    var width = landscape ? PredefinedPaper.Height : PredefinedPaper.Width;
                    var height = landscape ? PredefinedPaper.Width : PredefinedPaper.Height;
                    SelectedPaper = new PaperSize() { Width = width, Height = height };
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}